package hyl.base.medium;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;

//import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * 可定制化的图像显示基础窗口
 * 
 * @author 37798955@qq.com
 *
 */
public class ImageFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	ImagePanel lbl;
	boolean isopen = true;

	public ImageFrame(String 标题, int 宽, int 高) {
		super(标题);
		// setSize(宽, 高);
		// this.getRootPane().
		setSize(宽, 高);
		lbl = new ImagePanel();
		lbl.setSize(宽, 高);
		this.getRootPane().add(lbl);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				super.windowClosing(e);
				// 加入动作
				//System.out.println("关闭了1");
				isopen = false;
			}
		});

		this.setVisible(true);
		//JFrame.EXIT_ON_CLOSE 不能用,用了就会关闭 程序
		//隐藏而不关闭
		//this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE );

		// this.pack();
	}

	public boolean f显示(BufferedImage image) {
		// icon = new ImageIcon(image);
		if (isopen) {		
			// Graphics2D g2 = (Graphics2D)image.getGraphics();
			lbl.setImage(image);
			return true;
		}else {
			//System.out.println("关闭了2");
			return false;
		}
		// icon.setImage(image);
		// lbl.setIcon(icon);
	}

	class ImagePanel extends JPanel {
		private static final long serialVersionUID = 1L;
		private BufferedImage sourceImage;

		@Override
		protected void paintComponent(Graphics g) {
			Graphics2D g2d = (Graphics2D) g;
			g2d.clearRect(0, 0, this.getWidth(), this.getHeight());
			if (sourceImage != null) {
				g2d.drawImage(sourceImage, 0, 0, sourceImage.getWidth(), sourceImage.getHeight(), null);
			}
		}

		public BufferedImage getImage() {
			return sourceImage;
		}

		public void setImage(BufferedImage sourceImage) {
			this.sourceImage = sourceImage;
			this.repaint();
		}

	}
}